package com.ming.attendance.configuration;

import lombok.SneakyThrows;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * QuartzConfig 作用：
 * 配置Quartz，将自定义的JobFactory装入scheduler
 * 从而使得在 Quartz Job 中可以使用@Autowired
 */
@Configuration
public class QuartzConfig {
    @Autowired
    private CustomJobFactory customJobFactory;

    @SneakyThrows   // @SneakyThrows 作用：不需要担心Exception的处理
    @Bean
    public Scheduler scheduler(){
        StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        // 将自定义的JobFactory装入Scheduler，使得在Quartz Job中可以使用@Autowired
        scheduler.setJobFactory(customJobFactory);
        scheduler.start();
        return scheduler;
    }
}
